package cn.tedu._06vehicle.controller;


import cn.tedu._06vehicle.base.response.JsonResult;
import cn.tedu._06vehicle.base.response.StatusCode;
import cn.tedu._06vehicle.mapper.UserMapper;
import cn.tedu._06vehicle.pojo.dto.UserLoginParam;
import cn.tedu._06vehicle.pojo.dto.UserRegParam;
import cn.tedu._06vehicle.pojo.entity.User;
import cn.tedu._06vehicle.pojo.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@Slf4j
@RestController
@RequestMapping("v1/user/")
@Tag(name="01-用户信息管理模块")

public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @Operation(summary = "注册功能")
    @ApiOperationSupport(order = 10)

    //想要对封装参数进行校验，就将@Validated注解放在封装参数的位置上，然后在对应的实体类添加相应的校验规则
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("userRegParam="+userRegParam);
        String username = userRegParam.getUsername();
        int rows=userMapper.selectUserByUsername(username);
        if (rows>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user =new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUSer(user);
        return JsonResult.ok();

    }
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("login")

    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        log.debug("登录参数:" + userLoginParam);
        /*
            userVO情况1: null
            userVO情况2: {"id": 101, "username": "赵丽颖"}
         */
        UserVo userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO != null) { //登录成功
            //把用户的标识存储session会话中;
            session.setAttribute("user", userVO);
            return JsonResult.ok(userVO);
        }
        //用户名或密码错误
        return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
    }
    @GetMapping("currentUser")
    @Operation(summary = "获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(HttpSession session){
        UserVo userVo=(UserVo)session.getAttribute("user");
        return JsonResult.ok(userVo);
    }

    @GetMapping("logout")
    @Operation(summary = "退出功能")
    @ApiOperationSupport(order=40)
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }

}
